package com.itheima.travel.web;

import com.itheima.travel.req.BaseRequest;
import com.itheima.travel.res.GetwayRespons;
import com.itheima.travel.service.GatewayService;
import com.itheima.travel.session.SubjectUser;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.yaml.snakeyaml.events.Event;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Description 后台网关
 */
@Controller
@RequestMapping("/api-platform")
@Log4j2
public class ApiPlatformController {

    @Autowired
    GatewayService gatewayService;

    /**
     * 统一请求接口
     * @param service 服务
     * @param method 方法
     * @param file 文件上传
     * @param baseRequest 请求参数
     * @return
     */
    @RequestMapping(value = "/{service}/{method}")
    @ResponseBody
    //解决跨域问题
    @CrossOrigin
    public GetwayRespons apiLoan(@PathVariable("service") String service,
                                 @PathVariable("method") String method,
                                 MultipartFile file,
                                 @RequestBody BaseRequest baseRequest) {
        GetwayRespons getwayRespons = null;
        if (EmptyUtil.isNullOrEmpty(service)||EmptyUtil.isNullOrEmpty(method)) {
            log.warn("api-loan请求不合法！---> 调用者[{}]--->调用方法[{}]");
            return GetwayRespons.builder().data(null).code("1001").msg("参数不合法").build();
        } else {
            log.info("api-loan请求：调用类[{}]--->方法[{}]--->请求参数[{}]",
                    service, method, baseRequest.toString());
            getwayRespons = gatewayService.method(method, baseRequest,file);
        }
        return getwayRespons;
    }
}

